package net.tngou.crawler.util;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;

/**
 * 取得配置文件
 * @author tngou
 *
 */
public class ConfigurationUtil {

	
	public static File file(String  name) {
		Parameters params = new Parameters();
		@SuppressWarnings({ "rawtypes", "unchecked" })
		FileBasedConfigurationBuilder<?> builder =
			    new FileBasedConfigurationBuilder(FileBasedConfiguration.class)
			    .configure(params.properties()
			        .setFileName(name));
		URL url = builder.getFileHandler().getURL();
		
		try {
			File file = new File(url.toURI());
			return file;
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;	

	}
	
	
	public static Configuration config(String name) {
		Parameters params = new Parameters();
		FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
		    new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
		    .configure(params.properties()
		        .setFileName(name).setEncoding("UTF-8"));
			Configuration config;
			try {
				config = builder.getConfiguration();
				return config;
			} catch (ConfigurationException e) {
				e.printStackTrace();
				return null;
			}
			
	}
}
